package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.BsDepartDao;
import io.renren.modules.app.dao.BsEmployeeDao;
import io.renren.modules.app.dao.BsPostDao;
import io.renren.modules.app.entity.BsDepartEntity;
import io.renren.modules.app.entity.BsEmployeeEntity;
import io.renren.modules.app.entity.BsPostEntity;
import io.renren.modules.app.service.BsEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;


@Service("bsEmployeeService")
@Slf4j
public class BsEmployeeServiceImpl extends ServiceImpl<BsEmployeeDao, BsEmployeeEntity> implements BsEmployeeService {

    @Resource
    private BsEmployeeDao bsEmployeeDao ;

    @Resource
    private BsDepartDao bsDepartDao ;

    @Resource
    private BsPostDao bsPostDao ;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<BsEmployeeEntity> wrapper = new LambdaQueryWrapper<>();

        if (params.get("employeeName") != null && !params.get("employeeName").equals("")){
            wrapper.like(BsEmployeeEntity::getEmployeeName,params.get("employeeName"));
        }
        if (params.get("employeeAttribute") != null && !params.get("employeeAttribute").equals("")){
            wrapper.eq(BsEmployeeEntity::getEmployeeAttribute,params.get("employeeAttribute"));
        }

        IPage<BsEmployeeEntity> page = this.page(
                new Query<BsEmployeeEntity>().getPage(params),
                wrapper
        );
        //所属部门 岗位数据清洗
        for (BsEmployeeEntity record : page.getRecords()) {
            Long employeeDepart = Long.valueOf(record.getEmployeeDepart());//所属部门编号
            Long employeePost = Long.valueOf(record.getEmployeePost());//所属岗位编号
            BsDepartEntity bsDepartEntity = bsDepartDao.selectById(employeeDepart);
            BsPostEntity bsPostEntity = bsPostDao.selectById(employeePost);
            record.setDepartName(bsDepartEntity.getDepartName());
            record.setPostName(bsPostEntity.getPostName());
        }

        return new PageUtils(page);
    }

    @Override
    public synchronized void saveEmploy(BsEmployeeEntity bsEmployee) {
        try {
            if (bsEmployee.getEmployeeIdnum().length() != 18){
                throw new RRException("身份证号长度不合法");
            }
            //电话号校验
            if (!phoneValidate(bsEmployee.getEmployeePhone())){
                throw new RRException("电话号不合法");
            }
            //生成唯一员工域控账号  uuid+时间戳
            String time = String.valueOf(new Date().getTime());
            String uuid = UUID.randomUUID().toString().replace("-","");
            String employeeid = time + uuid ;
            bsEmployee.setEmployeeId(employeeid);
            bsEmployeeDao.insert(bsEmployee);
        }catch (Exception e){
            log.info("报错类及其所在方法：{}","BsEmployeeService --> saveEmploy");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    @Override
    public List<BsPostEntity> getPostByDepart(String departId) {
        LambdaQueryWrapper<BsPostEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BsPostEntity::getDepartId,departId);
        List<BsPostEntity> list = bsPostDao.selectList(wrapper);
        return list ;
    }

    @Override
    public BsPostEntity getPostById(String postId) {
        return bsPostDao.selectById(bsEmployeeDao.selectById(postId).getEmployeePost());
    }

    @Override
    public void updateEmploy(BsEmployeeEntity bsEmployee) {
        try {
            if (bsEmployee.getEmployeeIdnum().length() != 18){
                throw new RRException("身份证号长度不合法");
            }
            //电话号校验
            if (!phoneValidate(bsEmployee.getEmployeePhone())){
                throw new RRException("电话号不合法");
            }
            bsEmployeeDao.updateById(bsEmployee);
        }catch (Exception e){
            log.info("报错类及其所在方法：{}","BsEmployeeService --> updateEmploy");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }


    //校验电话号公共方法
    private boolean phoneValidate(String phoneNumber){
        if (phoneNumber != null && !phoneNumber.isEmpty()){
            boolean matches = Pattern.matches(
                    "^1[3-9]\\d{9}$|" +
                            "^1[3-9]\\d{1}[-\\s]\\d{4}[-\\s]\\d{4}$|" +
                            "^\\(1[3-9]\\d{1}\\)\\d{4}-\\d{4}$|" +
                            "^(?:\\(\\+\\d{2}\\)|\\+\\d{2})(\\d{11})$|" +
                            "^0\\d{3}-\\d{7}$|" +
                            "^0\\d{2}-\\d{8}$", phoneNumber);

            return matches;
        }
        return false;
    }

}
